import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

//创建一个线程个数固定的线程池
class MyThreadPool{
    private BlockingQueue<Runnable> queue = null;

    public MyThreadPool(int n ){
        //初始化线程池，创建固定个数线程池
        //这里使用ArrayBlockingQueue作为任务队列，容量为1000
        queue = new ArrayBlockingQueue<>(1000);

        //创建 n 个线程
        for (int i = 0; i < n; i++) {
            Thread thread = new Thread(() -> {
                try {
                    while (true){
                        //取出任务
                        Runnable rannable = queue.take();
                        rannable.run();
                    }
                }catch (InterruptedException e){
                    throw new RuntimeException();
                }
            });
            thread.start();
        }
    }

    public void submit(Runnable tesk) throws InterruptedException {
        //将任务放入队列中
        queue.put(tesk);
    }
}


public class Demo7 {

    public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool = new MyThreadPool(10);

        for (int i = 0; i < 1000; i++) {
            int id = i;
            //放入任务
            myThreadPool.submit(()->{
                System.out.println(Thread.currentThread().getName() + "id = " + id);
            });
        }
    }
}
